package delasell.adressBook.controler;


import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.DateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;

import delasell.adressBook.model.Contact;
import delasell.adressBook.view.*;

public class Listener implements ActionListener{
	
	public void actionPerformed(ActionEvent e)
	{
		if(e.getSource() == CreationWindow.getCancelButton())
		{
			AdressBook.newContact.setVisible(false);
		}
		
		if(e.getSource() == MainFrame.getNewButton())
		{
			createNewContact();			
		}
		
		if(e.getSource() == CreationWindow.getSaveButton())
		{
			saveNewContact();
		}
		
		if(e.getSource()==MainFrame.getSaveButton())
		{
			saveContactList();
		}
		if(e.getSource()==MainFrame.getDeleteButton())
		{
			deleteContact();
		}
		
		
	}
	
	public void saveNewContact()
	{
		String n = CreationWindow.getNameDisplay().getText();
		String fn = CreationWindow.getFirstNameDisplay().getText();
		String ad = CreationWindow.getPersAdressDisplay().getText();
		String cAd = CreationWindow.getCompAdressDisplay().getText();
		String persPhone1 = CreationWindow.getPersPhoneDisplay()[0].getText();
		String persPhone2 = CreationWindow.getPersPhoneDisplay()[1].getText();
		
		String compPhone1 = CreationWindow.getCompPhoneDisplay()[0].getText();
		String compPhone2 = CreationWindow.getCompPhoneDisplay()[1].getText();
		
		String w = CreationWindow.getWebDisplay().getText();
		String em = CreationWindow.getEmailDisplay().getText();
		
		String img=""; 
		
		AdressBook.cList.addContact(n, fn, ad, cAd, persPhone1, persPhone2, compPhone1, compPhone2, w, em, img);
		
		AdressBook.newContact.setVisible(false);
		
	}
	
	
	public void createNewContact()
	{
		CreationWindow.getNameDisplay().setText("Name");
		CreationWindow.getFirstNameDisplay().setText("First Name");
		CreationWindow.getPersAdressDisplay().setText("");
		CreationWindow.getCompAdressDisplay().setText("");
		CreationWindow.getPersPhoneDisplay()[0].setText("");
		CreationWindow.getPersPhoneDisplay()[1].setText("");
		
		CreationWindow.getCompPhoneDisplay()[0].setText("");
		CreationWindow.getCompPhoneDisplay()[1].setText("");
		
		CreationWindow.getWebDisplay().setText("");
		CreationWindow.getEmailDisplay().setText("");
		
		AdressBook.newContact.setVisible(true);
		
	}
	
	
	public void saveContactList()
	{		
		StringBuilder line = new StringBuilder("");
		Contact c = new Contact();
		String path;
		
		//while()
		//{
			line.append(c.getName()).append(";").append(c.getFirstName()).append(";").append(c.getAdress()).append(";").append(c.getCompanyAdress()).append(";").append(c.getPersonalPhone()[0]).append(";").append(c.getPersonalPhone()[1]).append(";").append(c.getProPhone()[0]).append(";").append(c.getProPhone()[1]).append(";").append(c.getWeb()).append(";").append(c.getEmail()).append(";\n");
			
		//}
			DateFormat d = DateFormat.getDateInstance(DateFormat.SHORT, Locale.FRANCE);
			System.out.println(d.format(new Date()));
			path = "C:/users/Bastien/Documents/Agenda/save/contactList" + d.toString() + ".csv";
			
			File f= new File(path);
			try {
				FileWriter fp = new FileWriter(f);
				System.out.println("open sucess");
				fp.write(line.toString());
				System.out.println("save sucess");
			} catch (IOException e) {
				e.printStackTrace();
				System.out.println("fail save");
			}			
		
	}
	
	public void deleteContact()
	{
		Contact current = new Contact();
		AdressBook.cList.getList().remove(current);
		
	}
	

}
